<template>
  <el-card>
    <div slot="header">
      <el-button type="primary" icon="el-icon-plus" size="small" @click="dialogFormVisible = true">添加</el-button>
    </div>
    <el-table :data="trademarkList" border style="width: 100%">
      <el-table-column type="index" align="center" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称">
      </el-table-column>
      <el-table-column label="品牌Logo">
        <template slot-scope="{ row, $index }">
          <img :src="row.logoUrl" style="width: 80px;height: 80px;">
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row,$index}">
          <el-button size="mini" type="warning" icon="el-icon-edit" @click="handleEdit($index, row)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete($index, row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
      :page-sizes="[3, 6, 9]" :page-size="100" layout="prev,pager, next,  jumper, ->,sizes,total " :total="total">
    </el-pagination>

    <el-dialog :title="tmForm.id ? '修改品牌' : '添加品牌'" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="tmForm">
        <el-form-item label="品牌名称" label-width="120px">
          <el-input v-model="tmForm.tmName" style="width: 400px;" size="small" placeholder="请输入品牌名称"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" label-width="120px">
          <el-upload class="avatar-uploader" action="http://gmall-h5-api.atguigu.cn/admin/product/upload"
            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">上传图片必须是jpg且不能大于2M</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelHandler">取 消</el-button>
        <el-button type="primary" @click="submitHandler">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { cloneDeep } from "lodash";
import trademarkApi from '@/api/trademark'
export default {
  name: 'Trademark',
  data() {
    return {
      trademarkList: [],
      total: 0,
      page: 1,
      limit: 3,
      tmForm: {
        tmName: '',
        logoUrl: '',
        id: '',
      },
      dialogFormVisible: false,
    }
  },
  mounted() {
    this.getTrademarkList()
  },
  methods: {
    // 修改当前品牌的功能函数
    async updateBaseTrademark() {
      try {
        await trademarkApi.updateBaseTrademark(this.tmForm)
      } catch (error) {
        console.error(error)
      }
    },
    // 删除当前品牌的功能函数
    async removeBaseTrademark(id) {
      try {
        await trademarkApi.removeBaseTrademark(id)
      } catch (error) {
        console.error(error)
      }
    },
    // 新增品牌的功能函数
    async saveBaseTrademark() {
      try {
        await trademarkApi.saveBaseTrademark(this.tmForm)
      } catch (error) {
        console.error(error)
      }
    },
    // 点击遮罩层取消的回调
    cancelHandler() {
      this.dialogFormVisible = false
      this.tmForm = {
        tmName: '',
        logoUrl: ''
      }
    },
    // 点击遮罩层确定的回调
    submitHandler() {
      const { tmName, logoUrl, id } = this.tmForm
      if (tmName && logoUrl) {
        if (id) {
          this.updateBaseTrademark()
        } else {
          this.saveBaseTrademark()
        }
        this.cancelHandler()
        this.page = 1
        this.getTrademarkList();
      } else {
        this.$message({
          showClose: true,
          message: '品牌名称与品牌logo不能为空!',
          type: 'warning'
        });
      }


    },
    // 点击编辑的回调
    handleEdit(index, row) {
      this.dialogFormVisible = true
      this.tmForm = cloneDeep(row)
    },
    // 点击删除的回调
    handleDelete(index, row) {
      this.$confirm(`确认要删除[${row.tmName}]吗?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.removeBaseTrademark(row.id)
        this.page = 1
        this.getTrademarkList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      })
    },
    // 获取品牌信息的功能函数
    async getTrademarkList() {
      try {
        let result = await trademarkApi.getTrademarkList(this.page, this.limit)
        this.trademarkList = result.data.records
        this.total = result.data.total
      } catch (error) {
        console.error(error)
      }
    },
    handleSizeChange(val) {
      this.limit = val
      this.getTrademarkList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getTrademarkList()
    },
    initTmForm() {
      return {
        tmName: '',
        logoUrl: ''
      }
    },
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }

  },
  compute: {

  },
  watch: {

  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>